package com.ziyi.tiantianshuiyin.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;


import com.android.tu.loadingdialog.LoadingDailog;
import com.gtdev5.geetolsdk.mylibrary.base.BaseGTActivity;
import com.gtdev5.geetolsdk.mylibrary.widget.CenterDialog;
import com.gtdev5.geetolsdk.mylibrary.widget.OnDialogClickListener;
import com.gyf.immersionbar.ImmersionBar;
import com.ziyi.tiantianshuiyin.R;
import com.ziyi.tiantianshuiyin.view.TitleView;

import butterknife.ButterKnife;

public abstract class BaseActivity extends BaseGTActivity implements View.OnClickListener {
    public Bundle save;
        public LoadingDailog dialog;
    private static final int DEFAULT_TIMEOUT = 6;

    protected TitleView title;

    private boolean showStatusBar = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        save = savedInstanceState;
        //设置为无标题栏
        setRootView();
        ButterKnife.bind(this);
        getLoadingDailog();
        //公共title
        title = (TitleView) findViewById(R.id.title);
        if (title != null) {
            title.setLeftButton(new TitleView.OnLeftButtonClickListener() {
                @Override
                public void onClick(View button) {
                    finish();
                }
            });
        }
        if (showStatusBar) {
            ImmersionBar.with(this)
                    .statusBarColor(R.color.white)
                    .statusBarDarkFont(true)
                    .fitsSystemWindows(true)
                    .navigationBarDarkIcon(false)
                    .init();
        }else{
            ImmersionBar.with(this).statusBarDarkFont(true).fullScreen(false).init();
        }
        initTitle();
        initView();
        initData();
    }

    public void getLoadingDailog() {
        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(this)
                .setMessage("正在加载")
                .setCancelable(true)
                .setCancelOutside(true);

        dialog = loadBuilder.create();
    }

    /**
     * 提示弹框
     */
    public void ShowTipDialog(String name, String content, String btnText, OnDialogClickListener listener) {
        int[] ids = new int[]{
                R.id.dialog_bt_dis,
                R.id.dialog_bt_ok
        };
        CenterDialog dialog = new CenterDialog(this, R.layout.dialog_show_tips, ids, false);
        dialog.setOnCenterClickListener((dial, view) -> {
            if (view.getId() == R.id.dialog_bt_ok) {
                listener.OnDialogOK();
                if (!isFinishing()) {
                    dialog.dismiss();
                }
            }
            if (view.getId() == R.id.dialog_bt_dis) {
                if (!isFinishing()) {
                    dialog.dismiss();
                }
                listener.OnDialogExit();
            }
        });
        if (!isFinishing()) {
            dialog.show();
            dialog.setText(R.id.dialog_tv_title, name);
            dialog.setText(R.id.dialog_tv_text, content);
            dialog.setText(R.id.dialog_bt_ok, btnText);
        }
    }

    public void setShowStatusBar(boolean show) {
        this.showStatusBar = show;
    }

    protected final void logShow(String msg) {
        Log.e("weather", msg);
    }

    protected abstract void initView();

    public abstract void initTitle();

    public abstract void setRootView();

    public abstract void initData();

    protected final String getTextByEditText(EditText et) {
        return et.getText().toString().trim();
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void finish() {
        hideSoftInputFromWindow(this);
        super.finish();
    }

    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    private void hideSoftInputFromWindow(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity
                .getSystemService(INPUT_METHOD_SERVICE);
        try {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
